<?php
require_once("database.php");
require_once '../DTO/THONGTINVON_DTO.php';
class xl_thong_tin_von extends database
{
	function danh_sach_theo_loai( $vi_tri, $so_luong, $ma_loai, $ma_lhkd )
	{
		$sql = "SELECT dn . *, ttv.ma as ma_von, ttv.von_huy_dong
                FROM doanh_nghiep dn, loai_hinh_kinh_doanh lhkd, phieu_dang_ky pdk, loai_phieu lp,thong_tin_von ttv
                WHERE dn.loai_hinh_dn = lhkd.ma
                AND pdk.ma_dn = dn.ma
                AND pdk.loai_phieu = lp.ma
                AND pdk.ma_von = ttv.ma
                AND lp.ma = '$ma_loai'
                AND lhkd.ma = '$ma_lhkd'
                LIMIT $vi_tri, $so_luong ";
		$this->setQuery($sql);
		return $this->loadAllRow();
	}

    function danh_sach_theo_loai_all( $vi_tri, $so_luong, $ma_loai )
    {
        $sql = "SELECT dn . *, ttv.ma as ma_von, ttv.von_huy_dong
                FROM doanh_nghiep dn, loai_hinh_kinh_doanh lhkd, phieu_dang_ky pdk, loai_phieu lp,thong_tin_von ttv
                WHERE dn.loai_hinh_dn = lhkd.ma
                AND pdk.ma_dn = dn.ma
                AND pdk.loai_phieu = lp.ma
                AND pdk.ma_von = ttv.ma
                AND lp.ma = '$ma_loai'
                LIMIT $vi_tri, $so_luong ";
        $this->setQuery($sql);
        return $this->loadAllRow();
    }

    function xem_chi_tiet($ma)
    {
        $ma = intval($ma);

        //$sql = "select * from thong_tin_von where ma = '$ma'";
        $sql = "SELECT dn . *, ttv.ma as ma_von, ttv.von_huy_dong
        FROM doanh_nghiep dn, loai_hinh_kinh_doanh lhkd, phieu_dang_ky pdk, loai_phieu lp,thong_tin_von ttv
        WHERE dn.loai_hinh_dn = lhkd.ma
        AND pdk.ma_dn = dn.ma
        AND pdk.loai_phieu = lp.ma
        AND pdk.ma_von = ttv.ma
        AND ttv.ma = '$ma'";
        $this->setQuery($sql);
        return $this->loadRow();
    }

	function danh_sach_tim_kiem($key, $vi_tri, $so_luong)
	{
		$sql = "select * from thong_tin_von where ten like '%$key%' limit $vi_tri, $so_luong";
		$this->setQuery($sql);
		return $this->loadAllRow();
	}
	function danh_sach_ban_chay()
	{
		$sql = "SELECT sp.*, ct.ma_thong_tin_von, sum(ct.so_luong) tong_so_luong
				FROM thong_tin_von sp, chi_tiet_hoa_don ct
				WHERE sp.ma = ct.ma_thong_tin_von
				GROUP BY ct.ma_thong_tin_von
				ORDER BY tong_so_luong DESC
				LIMIT 0, 10";
		$this->setQuery($sql);
		return $this->loadAllRow();
	}
	function danh_sach_cung_loai($ma, $ma_loai_thong_tin_von)
	{
		$sql = "select * from thong_tin_von where ma_loai_thong_tin_von = $ma_loai_thong_tin_von and ma != $ma ORDER BY RAND() limit 0, 6";
		$this->setQuery($sql);
		return $this->loadAllRow();
	}
	function danh_sach_theo_thong_tin_von_moi()
	{
		$sql = "select * from thong_tin_von where thong_tin_von_moi = 1 ";
		$this->setQuery($sql);
		return $this->loadAllRow();
	}
	function danh_sach($vi_tri, $so_luong)
	{
		$vi_tri = intval($vi_tri);
		$so_luong = intval($so_luong);
		$sql = "select * from thong_tin_von limit $vi_tri, $so_luong";
		$this->setQuery($sql);
		return $this->loadAllRow();
	}
	//function danh_sach()
	//{
	//	$sql = "select * from thong_tin_von ";
	//	$this->setQuery($sql);
	//	return $this->loadAllRow();
	//}
	function so_luong($ma_loai_thong_tin_von)
	{
		$sql = "select count(*) from thong_tin_von where ma_loai_thong_tin_von = '$ma_loai_thong_tin_von' ";
		$this->setQuery($sql);
		return $this->loadResult();
	}

	function xoa($ma)
	{
		$ma = intval($ma);

		$sql = "delete from thong_tin_von where ma = '$ma'";
		$this->setQuery($sql);
		return $this->query();
	}
	function them(ThongTinVonDTO $info)
	{
		$info->muc_lai_suat = addslashes($info->muc_lai_suat);
        $info->muc_lai_suat = intval($info->muc_lai_suat);

        $info->thoi_han = addslashes($info->thoi_han);
        $info->thoi_han = intval($info->thoi_han);

        $info->von_huy_dong = addslashes($info->von_huy_dong);
        $info->von_huy_dong = intval($info->von_huy_dong);

        $info->tai_khoan_the_chap = addslashes($info->tai_khoan_the_chap);
        $info->tai_khoan_the_chap = intval($info->tai_khoan_the_chap);



        $sql = "INSERT INTO thong_tin_von VALUES
                        (
                            '$info->thoi_han',
                            '$info->muc_lai_suat',
                            '$info->von_huy_dong',
                            '$info->tai_khoan_the_chap',
                            0
                        )";
		$this->setQuery($sql);
		return $this->query();
	}
	
	function cap_nhat($ma, $ten, $ma_loai_thong_tin_von, $mo_ta_tom_tat, $mo_ta_chi_tiet, $don_gia, $hinh, $thong_tin_von_moi, $so_lan_xem, $ngay_tao, $trang_thai)
	{
		$ma = intval($ma);
		$ten = addslashes($ten);
		$ma_loai_thong_tin_von = intval($ma_loai_thong_tin_von);
		$mo_ta_tom_tat = addslashes($mo_ta_tom_tat);
		$mo_ta_chi_tiet = addslashes($mo_ta_chi_tiet);
		$don_gia = intval($don_gia);
		$hinh = addslashes($hinh);
		$so_lan_xem = intval($so_lan_xem);

		$sql ="UPDATE thong_tin_von SET ten= '$ten', ma_loai_thong_tin_von= '$ma_loai_thong_tin_von', mo_ta_tom_tat= '$mo_ta_tom_tat', mo_ta_chi_tiet= '$mo_ta_chi_tiet', don_gia= '$don_gia', hinh= '$hinh', thong_tin_von_moi= '$thong_tin_von_moi', so_lan_xem= '$so_lan_xem', ngay_tao= '$ngay_tao', trang_thai= '$trang_thai' WHERE ma = '$ma'";
		$this->setQuery($sql);
		return $this->query();
	}
	
	function cap_nhat_trang_thai($ma)
	{
		$ma = intval($ma);

		$sql = "UPDATE thong_tin_von SET trang_thai = 1 - trang_thai WHERE ma = '$ma'";
		$this->setQuery($sql);
		return $this->query();
	}
	
	function cap_nhat_thong_tin_von_moi($ma)
	{
		$ma = intval($ma);

		$sql = "UPDATE thong_tin_von SET thong_tin_von_moi = 1 - thong_tin_von_moi WHERE ma = '$ma'";
		$this->setQuery($sql);
		return $this->query();
	}
	
	function tim_kiem ($tu_khoa)
	{
		//$sql = "select * from thong_tin_von where	$ten like %tu_khoa% or 	$mo_ta_tom_tat like %tu_khoa% or 	$mo_ta_chi_tiet like %tu_khoa% or 	$hinh like %tu_khoa%";
		//$this->setQuery($sql);
		return $this->loadAllRow();	
	}
 }
?>